package com.huluobo.user.pages

import android.text.TextUtils
import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Checkbox
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.lib_base.BaseTitle
import com.huluobo.lib_base.RoutePath
import com.huluobo.lib_base.UIState
import com.huluobo.lib_base.entity.User
import com.huluobo.lib_http.Const
import com.huluobo.user.R
import com.huluobo.user.UserIntent
import com.huluobo.user.UserViewModel
import com.hyphenate.chat.EMClient
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

/**
 *  Created by LC on 2025/7/22.
 */

@Composable
fun MinePage(navHostController: NavHostController, vm: UserViewModel = hiltViewModel()) {
    val mmkv = MMKV.defaultMMKV()
    //通过mmkv读取用户名,如果没有取到显示未登录
    var name by remember {
        mutableStateOf(mmkv.decodeString(Const.PARAM_USERNAME, "未登录").toString())
    }
    //通过mmkv读取用户角色,如果没取到就是0
    var role by remember {
        mutableStateOf(mmkv.decodeString(Const.PARAM_ROLE, "员工").toString())
    }

    //控制退出dialog的弹窗
    var isShow by remember {
        mutableStateOf(false)
    }

    Column(Modifier.padding(20.dp)) {
        Text(text = "我的", fontSize = 26.sp, fontWeight = FontWeight.Bold)
        Spacer(modifier = Modifier.height(20.dp))
        Card(
            border = BorderStroke(1.dp, Color.LightGray), modifier = Modifier
                .fillMaxWidth()
                .clickable {
                    if (name == "未登录") {
                        navHostController.navigate(RoutePath.LOGIN)
                    } else {
                        isShow = true
                    }
                }, colors = CardDefaults.cardColors(
                containerColor = Color.White
            )
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
            ) {
                AsyncImage(
                    model = "https://img0.baidu.com/it/u=788800651,2030194952&fm=253&app=120&size=w931&n=0&f=JPEG&fmt=auto?sec=1730307600&t=9c17b588a934b0ccf76349e5e5b7ca08",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(20.dp))
                Column(Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround) {
                    Text(text = name)
                    Text(text = role)
                }
            }
        }

        Spacer(modifier = Modifier.height(10.dp))
        Card(
            border = BorderStroke(1.dp, Color.LightGray),
            modifier = Modifier.fillMaxWidth(),
            colors = CardDefaults.cardColors(
                containerColor = Color.White
            )
        ) {
            Row(
                Modifier
                    .fillMaxWidth()
                    .padding(20.dp)
            ) {
                Column(Modifier.clickable {
                    //跳转到请假列表页面
                    navHostController.navigate(RoutePath.APPROVE_LIST)
                }) {
                    Image(
                        painter = painterResource(id = R.drawable.report),
                        contentDescription = "",
                        modifier = Modifier.size(30.dp)
                    )
                    Spacer(modifier = Modifier.height(10.dp))
                    Text(text = "请假")
                }
                Spacer(modifier = Modifier.width(10.dp))
                Column(Modifier.clickable {
                    //跳转到审批列表页面
                    navHostController.navigate(RoutePath.COM_LIST)
                }) {
                    Image(
                        painter = painterResource(id = R.drawable.question),
                        contentDescription = "",
                        modifier = Modifier.size(30.dp)
                    )
                    Spacer(modifier = Modifier.height(10.dp))
                    Text(text = "审批")
                }
                Spacer(modifier = Modifier.width(10.dp))
                Column(Modifier.clickable {
                    //跳转到审批列表页面
                    navHostController.navigate(RoutePath.VIDEO_COLLECT)
                }) {
                    Image(
                        painter = painterResource(id = R.drawable.question),
                        contentDescription = "",
                        modifier = Modifier.size(30.dp)
                    )
                    Spacer(modifier = Modifier.height(10.dp))
                    Text(text = "收藏")
                }
            }
        }
    }

    if (isShow) {
        AlertDialog(onDismissRequest = { isShow = false }, confirmButton = {
            Text(text = "确认", modifier = Modifier.clickable {
                mmkv.remove(Const.PARAM_USERNAME)
                mmkv.remove(Const.PARAM_ROLE)
                name = "未登录"
                role = "员工"
                EMClient.getInstance().logout(true)
                isShow = false
            })
        }, title = { Text(text = "退出提醒") }, text = { Text(text = "是否退出当前账号?") })
    }
}